package com.atguigu.srb.core.service.impl;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.common.utils.JwtUtils;
import com.atguigu.common.utils.MD5;
import com.atguigu.srb.core.mapper.UserAccountMapper;
import com.atguigu.srb.core.mapper.UserLoginRecordMapper;
import com.atguigu.srb.core.pojo.entity.UserAccount;
import com.atguigu.srb.core.pojo.entity.UserInfo;
import com.atguigu.srb.core.mapper.UserInfoMapper;
import com.atguigu.srb.core.pojo.entity.UserLoginRecord;
import com.atguigu.srb.core.pojo.vo.LoginVO;
import com.atguigu.srb.core.pojo.vo.RegisterVO;
import com.atguigu.srb.core.pojo.vo.UserInfoVO;
import com.atguigu.srb.core.service.UserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户基本信息 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2022-09-13
 */
@Service
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfo> implements UserInfoService {

    @Resource
    RedisTemplate redisTemplate;
    @Resource
    UserInfoMapper userInfoMapper;
    @Resource
    UserAccountMapper userAccountMapper;
    @Resource
    UserLoginRecordMapper userLoginRecordMapper;
    @Override
    public void register(RegisterVO registerVO) {
        String code = registerVO.getCode();
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        Integer userType = registerVO.getUserType();
        Assert.notNull(code, ResponseEnum.CODE_NULL_ERROR);
        Assert.notNull(mobile,ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notNull(password,ResponseEnum.PASSWORD_NULL_ERROR);

       /* String codeCache = (String)redisTemplate.opsForValue().
                get("srb:sms:code:" + mobile);*/

       // Assert.isTrue(code.equals(codeCache),ResponseEnum.CODE_ERROR);
        UserInfo userInfo =new UserInfo();
        userInfo.setUserType(userType);
        userInfo.setName(mobile);
        userInfo.setNickName(mobile);
        userInfo.setPassword(MD5.encrypt(password));
        userInfo.setMobile(mobile);
        userInfoMapper.insert(userInfo);

        UserAccount userAccount = new UserAccount();
        userAccount.setUserId(userInfo.getId());
        userAccountMapper.insert(userAccount);
    }

    @Override
    public UserInfoVO login(LoginVO loginVO,String ip) {
        //获取前段传过来的参数
        String mobile = loginVO.getMobile();
        String password = loginVO.getPassword();
        Integer userType = loginVO.getUserType();
        //参数检验
        Assert.notNull(mobile,ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notNull(password,ResponseEnum.PASSWORD_NULL_ERROR);

        //查询数据库中的数据
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>()
                .eq("user_type",userType)
                .eq("mobile",mobile);
        UserInfo userInfo = baseMapper.selectOne(queryWrapper);

        //对前端密码加密
        String encryptPwd = MD5.encrypt(password);
        //判断用户存不存在
        Assert.notNull(userInfo,ResponseEnum.LOGIN_MOBILE_ERROR);
        //密码检验
        Assert.isTrue(encryptPwd.equals(userInfo.getPassword()),ResponseEnum.LOGIN_PASSWORD_ERROR);

        //生成登录令牌
        String token = JwtUtils.createToken(userInfo.getId(), userInfo.getName());

        //封装结果返回前端
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setToken(token);
        userInfoVO.setHeadImg(userInfo.getHeadImg());
        userInfoVO.setMobile(mobile);
        userInfoVO.setName(userInfo.getName());
        userInfoVO.setUserType(userType);
        userInfoVO.setNickName(userInfo.getNickName());
        //记录登录日志
        UserLoginRecord userLoginRecord = new UserLoginRecord();
        userLoginRecord.setUserId(userInfo.getId());
        userLoginRecord.setIp(ip);
        userLoginRecordMapper.insert(userLoginRecord);

        return userInfoVO;
    }

    @Override
    public boolean checkMobile(String mobile) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<UserInfo>()
                .eq("mobile",mobile);
        Integer integer = baseMapper.selectCount(queryWrapper);
        if (integer >0){
            return false;
        }
        return true;
    }
}
